package com.html;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import android.annotation.TargetApi;
import android.content.Context;
import android.util.Log;

import com.application.VideosList;
import com.db.DBConnect;

public class FLVCDResolve {
	private Context context;
	private Document doc2;
	private VideosList videoList;

	public FLVCDResolve(Context context) {
		this.context = context;
		videoList = (VideosList) context.getApplicationContext();
	}

	@TargetApi(8)
	public String getPlayUrl(String url, String sharpnessFlag) {
		String defaultUrl = url.replace(url,
				"http://www.flvcd.com/parse.php?kw=" + url);
		String finaUrl = url.replace(url, "http://www.flvcd.com/parse.php?kw="
				+ url + "&format=high");
		String SuperfinaUrl = url.replace(url,
				"http://www.flvcd.com/parse.php?kw=" + url + "&format=super");
		List<String> b = new ArrayList<String>();
		List<String> c = new ArrayList<String>();
		List<String> d = new ArrayList<String>();
		int i = 0;
		try {
			doc2 = Jsoup.connect(defaultUrl).timeout(60000).get();
			Elements content = doc2.select("td");
			for (Element link : content) {
				for (Element element : link.getElementsByTag("font")) {
					if (element.children().hasText())
						i++;
				}
				if (link.children().select("a").hasAttr("onclick")) {
					for (int j = 0; j < link.select("a").toArray().length; j++) {
						String videoUrl = link.select("a").toArray()[j]
								.toString();
						b.add(videoUrl);
					}
				}
			}
			videoList.setSharpness(i);

		} catch (IOException e) {
			e.printStackTrace();
		}
		try {
			doc2 = Jsoup.connect(finaUrl).timeout(60000).get();
			Elements content = doc2.select("td");
			for (Element link : content) {
				if (link.children().select("a").hasAttr("onclick")) {
					for (int j = 0; j < link.select("a").toArray().length; j++) {
						String videoUrl = link.select("a").toArray()[j]
								.toString();
						c.add(videoUrl);
					}
				}
			}

		} catch (IOException e) {
			e.printStackTrace();
		}
		try {
			doc2 = Jsoup.connect(SuperfinaUrl).timeout(60000).get();
			Elements content = doc2.select("td");
			for (Element link : content) {
				if (link.children().select("a").hasAttr("onclick")) {
					for (int j = 0; j < link.select("a").toArray().length; j++) {
						String videoUrl = link.select("a").toArray()[j]
								.toString();
						d.add(videoUrl);
					}
				}
			}

		} catch (IOException e) {
			e.printStackTrace();
		}
		videoList.setRealLowUrlArray(b);
		videoList.setRealHighUrlArray(c);
		videoList.setRealSuperUrlArray(d);
		return "";
	}

	@TargetApi(8)
	public String getPlayUrl(String url, String sharpnessFlag, int sharpness) {
		String defaultUrl = url.replace(url,
				"http://www.flvcd.com/parse.php?kw=" + url);
		String finaUrl = url.replace(url, "http://www.flvcd.com/parse.php?kw="
				+ url + "&format=high");
		String SuperfinaUrl = url.replace(url,
				"http://www.flvcd.com/parse.php?kw=" + url + "&format=super");
		List<String> b = new ArrayList<String>();
		int i = 0;
		int s = 0;
		try {
			doc2 = Jsoup.connect(defaultUrl).timeout(60000).get();
			Elements content = doc2.select("td");
			for (Element link : content) {
				for (Element element : link.getElementsByTag("font")) {
					if (element.children().hasText())
						i++;
				}
				if (link.children().select("a").hasAttr("onclick")) {
					for (int j = 0; j < link.select("a").toArray().length; j++) {
						String videoUrl = link.select("a").toArray()[j]
								.toString();
						b.add(videoUrl);
					}
				}
			}
			videoList.setSharpness(i);

		} catch (IOException e) {
			e.printStackTrace();
		}
		s = getPlayableSharpness(i, sharpness);
		switch (s) {
		case 0:
			videoList.setRealLowUrlArray(b);
			break;
		case 1:
			try {
				b.clear();
				doc2 = Jsoup.connect(finaUrl).timeout(60000).get();
				Elements content = doc2.select("td");
				for (Element link : content) {
					if (link.children().select("a").hasAttr("onclick")) {
						for (int j = 0; j < link.select("a").toArray().length; j++) {
							String videoUrl = link.select("a").toArray()[j]
									.toString();
							b.add(videoUrl);
						}
					}
				}

			} catch (IOException e) {
				e.printStackTrace();
			}
			videoList.setRealHighUrlArray(b);
			break;
		case 2:
			try {
				b.clear();
				doc2 = Jsoup.connect(SuperfinaUrl).timeout(60000).get();
				Elements content = doc2.select("td");
				for (Element link : content) {
					if (link.children().select("a").hasAttr("onclick")) {
						for (int j = 0; j < link.select("a").toArray().length; j++) {
							String videoUrl = link.select("a").toArray()[j]
									.toString();
							b.add(videoUrl);
						}
					}
				}

			} catch (IOException e) {
				e.printStackTrace();
			}
			videoList.setRealSuperUrlArray(b);
			break;
		}
		return "";
	}

	private int getPlayableSharpness(int net, int local) {
		if(net>local){
			return local;
		}else if(local >net){
			return net;
		}else{
			return net;
		}
	
		

	}

	private int getListSize(Elements content) {
		int i = 0;
		for (Element link : content) {
			if (link.children().select("a").hasAttr("onclick")) {

				i = link.select("a").size();
				Log.v("html", link.select("a") + "");
				Log.v("length", i + "");
			}
		}
		Log.v("size", i + "");
		return i;
	}
}
